// pages/deatil/detail.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgsrv: app.globalData.srvlink + '/diancan/img/',
    goodsInfo: {}
  },

  onLoad: function (e) {
    var id = e.id;
    this.getGoods(id);
  },
  wxpay: function () {
    var that=this
    var allprice = this.data.goodsInfo.price;
    var userData = wx.getStorageSync("userInfo")
    var openId = wx.getStorageSync("openId")
   
    wx.showModal({
      title: '提示',
      content: '支付金额为：' + allprice + '元,确定要支付吗？',
      success: function (resconf) {
        if (resconf.confirm) {
          var code = null;
          var orderData = {};
          orderData.amount = allprice;
          orderData.qty = 1
          orderData.userId = openId;
          orderData.userName = userData.nickName;
          orderData.tableNO = app.globalData.tableNo;
          orderData.tableName = app.globalData.tableNo;
          orderData.goodsId = that.data.goodsInfo.goodsId
          orderData.goodsName = that.data.goodsInfo.goodsName
          orderData.imgUrl = that.data.goodsInfo.img


          wx.request({
            url: app.globalData.srvlink + "/diancan/order",
            data: orderData,
            method: 'POST',
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              //ok
              if (res.data.code == '1') {
                //发起支付                
                that.xiadan(res.data.data);
              }

            }
          });


        } else {
          console.log('用户点击取消')

        }

      }
    })
  },

  xiadan: function (orderInfo) {
     
    var that = this
    var data = {}
    data.body = this.data.goodsInfo.goodsName
    data.attach = orderInfo.orderNO
    data.outTradeNo = orderInfo.orderNO
    data.totalFee =orderInfo.amount*100
    data.openid = orderInfo.userId

    wx.showLoading()
    wx.request({
      url: app.globalData.srvlink + '/diancan/h5pay/do',
      data: data,
      method: 'POST',
      header: {
        'content-type': 'application/json'         
      },
      success: function (res) {
        if(res.data.code==0){
          wx.requestPayment({
            'timeStamp': res.data.param.timeStamp + '',
            'nonceStr': res.data.param.nonceStr,
            'package': res.data.param.package,
            'signType': 'MD5',
            'paySign': res.data.param.paySign,
            'success': function (res) {
              console.info(res)
              wx.showToast({
                title: '支付成功',
              })
              wx.switchTab({
                url: '../index/index?' + 'tableNo=' + getApp().globalData.tableNo + '&businessmenNo=' + getApp().globalData.businessmenNo,
              })
            },
            'fail': function (res) {
              wx.showToast({
                title: '支付了',
              })
            }
          })
        }else{
          wx.showToast({
            title: '支付了',
          })
        }
              
        
      }
    })
  },

  getGoods: function (id) {
    var that = this

    wx.showLoading();
    wx.request({
      url: app.globalData.srvlink + '/diancan/goods/' + id,
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        wx.hideLoading()
        that.setData({ goodsInfo: res.data.data })

      }
    })
  },

  join: function () {
    var icon = 0;
    var   a = this.data.goodsInfo;
    for (let i = 0; i < app.globalData.joinCar.length; i++) {

    }
    if (icon == 0) {
      app.globalData.joinCar.push(a);
      console.log("添加餐饮成功")
      this.joinT();
    }
  },
  joinT: function () {
    wx.showToast({
      title: '成功添加餐饮至购物车',
      mask:true,
      icon:'success'
      
    })
  },

})
